//
//  UIImage+Extension.swift
//  照片选择器
//
//  Created by  NorthCity on 15/11/17.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit

extension UIImage{
    
    func scaleToWidth(width:CGFloat)->UIImage{
        
        // 1. 判断宽度
        if size.width < width {
            return self
        }
        
        // 2. 计算比例
        let height = size.height * width / size.width
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        // 3. 绘制图像
        // 1> 开启上下文
        UIGraphicsBeginImageContext(rect.size)
        
        // 2> 绘制图像
        drawInRect(rect)
        
        // 3> 获得结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        // 4> 关闭上下问
        UIGraphicsEndImageContext()
        
        // 5> 返回结果
        return result
    }
        
        
        
    }
    
    
